<template>
    <div class="selectpp-auditor">
        <Cascader :data="trees" size="small" @on-change="changed" :load-data="loadData" change-on-select placeholder="请选择部门"></Cascader>
        <br/>
        <Select v-model="department_position_id" size="small" @on-change="loadPerson" placeholder="请选择岗位">
            <Option :value="item.id" v-for="(item,ind) in stationInfo" :key="ind">{{item.position_name.zh?item.position_name.zh:item.position_name.mn}}</Option>
        </Select>
        <div style="min-height:250px;margin-top:20px;height:400px;overflow:auto">
            <RadioGroup @on-change="toselec">
                <div v-for="item in examiners" :key="item.id" style="margin-bottom:2px"><Radio :label="item.id">{{item.name.zh}}</Radio></div>
            </RadioGroup>
            <div style="text-align:center" v-if="Messagem">-----{{Message}}-----</div>
            <Spin size="large" fix v-if="spinShow"></Spin>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            Message:'请选择教师',
            Messagem:true,
            spinShow:false,


            department_position_id:null,
            department_id:null,
            stationInfo: [],
            examiners: [],
            // 菜单树
            trees: []
        }
    },
    methods: {

        
        /************************************************************
         * 
         * 加载一级部门
         * 
         ************************************************************/
        loadep() {
            this.spinShow=true
            this.$http.get('api/user/v1/department/info')
            .then(res=>{  
                this.trees = this.process(res.data)  
                this.spinShow=false       
            })
            .catch(err=>{
                if(err.code && err.code==422) {
                    this.$Message.error(err.message);
                }
                this.spinShow=false     
            })
        },

        /************************************************************
         * 
         * 处理数据
         * 
         ************************************************************/
        process(data) {
            let arr = []
            data.forEach(item => {
                item.title = item.label = item.department_name.zh?item.department_name.zh:item.department_name.mn
                item.value = item.id
                if(item.childs) {
                    item.loading = false,
                    item.children = []
                }
                arr.push(item)
            })
            return arr
        },

        /************************************************************
         * 
         * 异步加载二级部门
         * 
         ************************************************************/
        loadData(item, callback) {
            let condition = {
                pid: item.id
            }
            item.loading = true;
            this.$http.get('api/user/v1/department/info',{params:condition})
            .then(res=>{  
                item.children = this.process(res.data)
                item.loading = false;
                callback()        
            })
            .catch(err=>{
                if(err.code && err.code==422) {
                    this.$Message.error(err.message);
                }
            })
        },

        /************************************************************
         * 
         * 点击部门获取岗位数据
         * 
         ************************************************************/
        changed(value) {
            this.spinShow=true 
            this.department_id=id
            let condition = {
                department_id: value[value.length-1]
            }
            this.$http.get('api/user/v1/department/get/position/user/list',{params:condition})
            .then(res=>{  
                this.stationInfo = res.data  
                this.spinShow=false       
            })
            .catch(err=>{
                if(err.code && err.code==422) {
                    this.$Message.error(err.message);
                }
                this.spinShow=false 
            })
        },
        

        /************************************************************
         * 
         * 点击岗位获取教师数据
         * 
         ************************************************************/
        loadPerson(id) {
            if(this.stationInfo.length!=0) {
                let data = this.stationInfo.find((item)=> {
                    return item.id == id
                }) 
                if(data!=undefined){
                    this.examiners = data.users ? data.users : []
                    if(this.examiners.length==0){
                        this.Message='暂无数据',
                        this.Messagem=true
                    }else{
                        this.Messagem=false
                    }
                }
            }
        },

        /************************************************************
         * 
         * 点击教师数据返回数据
         * 
         ************************************************************/
        toselec(val) {
            let teacherinfo = this.examiners.find((item)=> {
                return item.id == val
            }) 
            let tp={}
                tp.department_position_id=this.department_position_id
                tp.department_id=this.department_id
                tp.teacher_id=val
                tp.teacher_name=teacherinfo.name
            this.$emit('on-select',tp)
        }
    },
    mounted() {
        this.loadep()
    }
}
</script>
